package net.siufung.market.base.user.controller.regulation;

import lombok.AllArgsConstructor;
import net.siufung.core.exception.BizServiceException;
import net.siufung.core.model.enums.InitializeEnum;
import net.siufung.core.model.regulation.AbstractRegulation;
import net.siufung.market.base.user.mapper.IUserMapper;
import net.siufung.market.base.user.mapper.entity.User;

import java.util.Objects;

/**
 * @author 陈建峰
 * @since 2022/4/18 2:09 下午
 */
@AllArgsConstructor
public class UserDeleteRegulation extends AbstractRegulation<Integer> {


    private static final long serialVersionUID = -5757288614616926127L;

    private final IUserMapper userMapper;

    @Override
    public boolean isSatisfiedBy(Integer userId) {
        if(Objects.isNull(userId)){
            throw new BizServiceException("用户主键为空！");
        }
        User user = userMapper.selectById(userId);
        if(Objects.isNull(user)){
            throw new BizServiceException(String.format("用户id: %s 不存在！", userId));
        }
        if(InitializeEnum.IS.getValue().equals(user.getInitFlag())){
            throw new BizServiceException(user.getUserName()+"已经初始化，不能删除，请禁用！");
        }
        return Boolean.TRUE;
    }
}
